package com.stylefeng.guns.core.node;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class List2Tree {
  public static void main(String[] args) {
    TreeNode t1 = new TreeNode("1", "p1", "0");
    TreeNode t2 = new TreeNode("2", "p2", "0");
    TreeNode t3 = new TreeNode("3", "s11", "1");
    TreeNode t4 = new TreeNode("4", "s12", "1");
    TreeNode t5 = new TreeNode("5", "s21", "2");
    TreeNode t6 = new TreeNode("6", "s22", "2");
    TreeNode t7 = new TreeNode("7", "s23", "2");
    List<TreeNode> treeList = new ArrayList<>();
    Collections.addAll(treeList, t1, t2, t3, t4, t5, t6, t7);
    System.out.println(toTree01(treeList));
    System.out.println(toTree02(treeList));
  }
  /** * 方式一：使用两层循环实现 * @param list * @return */

  public static List<TreeNode> toTree01(List<TreeNode> treeList) {
    List<TreeNode> retList = new ArrayList<>();
    for (TreeNode parent : treeList) {
      if ("0".equals(parent.getParentId())) {
        retList.add(parent);
      }
      for (TreeNode child : treeList) {
        if (child.getParentId() == parent.getId()) {
          if (parent.getChildren() == null) {
            parent.setChildren(new ArrayList<>());
          }
          parent.getChildren().add(child);
        }
      }
    }
    return retList;
  }
  /** * 方式二：使用递归实现 * @param treeList * @return */

  public static List<TreeNode> toTree02(List<TreeNode> treeList) {
    List<TreeNode> retList = new ArrayList<>();
    for (TreeNode parent : treeList) {
      if ("0".equals(parent.getParentId())) {
        retList.add(findChildren(parent, treeList));
      }
    }
    return retList;
  }

  private static TreeNode findChildren(TreeNode parent, List<TreeNode> treeList) {
    for (TreeNode child : treeList) {
      if (parent.getId().equals(child.getParentId())) {
        if (parent.getChildren() == null) {
          parent.setChildren(new ArrayList<>());
        }
        parent.getChildren().add(findChildren(child, treeList));
      }
    }
    return parent;
  }
}
